package reamodel.diagram.edit.parts;

import org.eclipse.draw2d.FigureUtilities;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPartFactory;
import org.eclipse.gef.tools.CellEditorLocator;
import org.eclipse.gmf.runtime.diagram.ui.editparts.ITextAwareEditPart;
import org.eclipse.gmf.runtime.draw2d.ui.figures.WrappingLabel;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Text;

import reamodel.diagram.part.REAmodelVisualIDRegistry;

/**
 * @generated
 */
public class REAmodelEditPartFactory implements EditPartFactory {

	/**
	 * @generated
	 */
	public EditPart createEditPart(EditPart context, Object model) {
		if (model instanceof View) {
			View view = (View) model;
			switch (REAmodelVisualIDRegistry.getVisualID(view)) {

			case REAModelEditPart.VISUAL_ID:
				return new REAModelEditPart(view);

			case EconomicResourceTypeEditPart.VISUAL_ID:
				return new EconomicResourceTypeEditPart(view);

			case EconomicResourceTypeNameEditPart.VISUAL_ID:
				return new EconomicResourceTypeNameEditPart(view);

			case EconomicAgentTypeEditPart.VISUAL_ID:
				return new EconomicAgentTypeEditPart(view);

			case EconomicAgentTypeNameEditPart.VISUAL_ID:
				return new EconomicAgentTypeNameEditPart(view);

			case DecrementEconomicEventTypeEditPart.VISUAL_ID:
				return new DecrementEconomicEventTypeEditPart(view);

			case DecrementEconomicEventTypeNameEditPart.VISUAL_ID:
				return new DecrementEconomicEventTypeNameEditPart(view);

			case IncrementEconomicEventTypeEditPart.VISUAL_ID:
				return new IncrementEconomicEventTypeEditPart(view);

			case IncrementEconomicEventTypeNameEditPart.VISUAL_ID:
				return new IncrementEconomicEventTypeNameEditPart(view);

			case EconomicAgentEditPart.VISUAL_ID:
				return new EconomicAgentEditPart(view);

			case EconomicAgentNameEditPart.VISUAL_ID:
				return new EconomicAgentNameEditPart(view);

			case EconomicResourceEditPart.VISUAL_ID:
				return new EconomicResourceEditPart(view);

			case EconomicResourceNameEditPart.VISUAL_ID:
				return new EconomicResourceNameEditPart(view);

			case DecrementEconomicEventEditPart.VISUAL_ID:
				return new DecrementEconomicEventEditPart(view);

			case DecrementEconomicEventNameEditPart.VISUAL_ID:
				return new DecrementEconomicEventNameEditPart(view);

			case IncrementEconomicEventEditPart.VISUAL_ID:
				return new IncrementEconomicEventEditPart(view);

			case IncrementEconomicEventNameEditPart.VISUAL_ID:
				return new IncrementEconomicEventNameEditPart(view);

			case IncrementCommitmentEditPart.VISUAL_ID:
				return new IncrementCommitmentEditPart(view);

			case IncrementCommitmentNameEditPart.VISUAL_ID:
				return new IncrementCommitmentNameEditPart(view);

			case DecrementCommitmentEditPart.VISUAL_ID:
				return new DecrementCommitmentEditPart(view);

			case DecrementCommitmentNameEditPart.VISUAL_ID:
				return new DecrementCommitmentNameEditPart(view);

			case EconomicResourceTypeTypifyEditPart.VISUAL_ID:
				return new EconomicResourceTypeTypifyEditPart(view);

			case TypifyLabelEditPart.VISUAL_ID:
				return new TypifyLabelEditPart(view);

			case OutflowEditPart.VISUAL_ID:
				return new OutflowEditPart(view);

			case OutflowNameEditPart.VISUAL_ID:
				return new OutflowNameEditPart(view);

			case EconomicResourceOutflowLabelEditPart.VISUAL_ID:
				return new EconomicResourceOutflowLabelEditPart(view);

			case InflowEditPart.VISUAL_ID:
				return new InflowEditPart(view);

			case InflowNameEditPart.VISUAL_ID:
				return new InflowNameEditPart(view);

			case EconomicResourceInflowLabelEditPart.VISUAL_ID:
				return new EconomicResourceInflowLabelEditPart(view);

			case DecrementEconomicEventTypeTypifyEditPart.VISUAL_ID:
				return new DecrementEconomicEventTypeTypifyEditPart(view);

			case TypifyLabel2EditPart.VISUAL_ID:
				return new TypifyLabel2EditPart(view);

			case IncrementEconomicEventTypeTypifyEditPart.VISUAL_ID:
				return new IncrementEconomicEventTypeTypifyEditPart(view);

			case TypifyLabel3EditPart.VISUAL_ID:
				return new TypifyLabel3EditPart(view);

			case OutflowTypeEditPart.VISUAL_ID:
				return new OutflowTypeEditPart(view);

			case OutflowTypeNameEditPart.VISUAL_ID:
				return new OutflowTypeNameEditPart(view);

			case OutflowTypeLabelEditPart.VISUAL_ID:
				return new OutflowTypeLabelEditPart(view);

			case InflowTypeEditPart.VISUAL_ID:
				return new InflowTypeEditPart(view);

			case InflowTypeNameEditPart.VISUAL_ID:
				return new InflowTypeNameEditPart(view);

			case InflowTypeLabelEditPart.VISUAL_ID:
				return new InflowTypeLabelEditPart(view);

			case EconomicAgentTypeTypifyEditPart.VISUAL_ID:
				return new EconomicAgentTypeTypifyEditPart(view);

			case TypifyLabel4EditPart.VISUAL_ID:
				return new TypifyLabel4EditPart(view);

			case ReceiveParticipationEditPart.VISUAL_ID:
				return new ReceiveParticipationEditPart(view);

			case ReceiveParticipationNameEditPart.VISUAL_ID:
				return new ReceiveParticipationNameEditPart(view);

			case ReceiveLabelEditPart.VISUAL_ID:
				return new ReceiveLabelEditPart(view);

			case ProvideParticipationEditPart.VISUAL_ID:
				return new ProvideParticipationEditPart(view);

			case ProvideParticipationNameEditPart.VISUAL_ID:
				return new ProvideParticipationNameEditPart(view);

			case ProvideLabelEditPart.VISUAL_ID:
				return new ProvideLabelEditPart(view);

			case IncrementEconomicEventDualityEditPart.VISUAL_ID:
				return new IncrementEconomicEventDualityEditPart(view);

			case DualityLabelEditPart.VISUAL_ID:
				return new DualityLabelEditPart(view);

			case IncrementEconomicEventTypeDualityTypeEditPart.VISUAL_ID:
				return new IncrementEconomicEventTypeDualityTypeEditPart(view);

			case DualityTypeLabelEditPart.VISUAL_ID:
				return new DualityTypeLabelEditPart(view);

			case CommitmentAgentEditPart.VISUAL_ID:
				return new CommitmentAgentEditPart(view);

			case SpecifyLabelEditPart.VISUAL_ID:
				return new SpecifyLabelEditPart(view);

			case CommitmentResourceEditPart.VISUAL_ID:
				return new CommitmentResourceEditPart(view);

			case SpecifyLabel2EditPart.VISUAL_ID:
				return new SpecifyLabel2EditPart(view);

			case IncrementCommitmentFulfillEditPart.VISUAL_ID:
				return new IncrementCommitmentFulfillEditPart(view);

			case FulfillLabelEditPart.VISUAL_ID:
				return new FulfillLabelEditPart(view);

			case DecrementCommitmentFulfillEditPart.VISUAL_ID:
				return new DecrementCommitmentFulfillEditPart(view);

			case FulfillLabel2EditPart.VISUAL_ID:
				return new FulfillLabel2EditPart(view);

			case IncrementCommitmentReciprocityEditPart.VISUAL_ID:
				return new IncrementCommitmentReciprocityEditPart(view);

			case ReciprocityLabelEditPart.VISUAL_ID:
				return new ReciprocityLabelEditPart(view);

			case DecrementCommitmentSpecifyEditPart.VISUAL_ID:
				return new DecrementCommitmentSpecifyEditPart(view);

			case SpecifyLabel3EditPart.VISUAL_ID:
				return new SpecifyLabel3EditPart(view);

			case IncrementCommitmentSpecifyEditPart.VISUAL_ID:
				return new IncrementCommitmentSpecifyEditPart(view);

			case SpecifyLabel4EditPart.VISUAL_ID:
				return new SpecifyLabel4EditPart(view);
			}
		}
		return createUnrecognizedEditPart(context, model);
	}

	/**
	 * @generated
	 */
	private EditPart createUnrecognizedEditPart(EditPart context, Object model) {
		// Handle creation of unrecognized child node EditParts here
		return null;
	}

	/**
	 * @generated
	 */
	public static CellEditorLocator getTextCellEditorLocator(
			ITextAwareEditPart source) {
		if (source.getFigure() instanceof WrappingLabel)
			return new TextCellEditorLocator((WrappingLabel) source.getFigure());
		else {
			return new LabelCellEditorLocator((Label) source.getFigure());
		}
	}

	/**
	 * @generated
	 */
	static private class TextCellEditorLocator implements CellEditorLocator {

		/**
		 * @generated
		 */
		private WrappingLabel wrapLabel;

		/**
		 * @generated
		 */
		public TextCellEditorLocator(WrappingLabel wrapLabel) {
			this.wrapLabel = wrapLabel;
		}

		/**
		 * @generated
		 */
		public WrappingLabel getWrapLabel() {
			return wrapLabel;
		}

		/**
		 * @generated
		 */
		public void relocate(CellEditor celleditor) {
			Text text = (Text) celleditor.getControl();
			Rectangle rect = getWrapLabel().getTextBounds().getCopy();
			getWrapLabel().translateToAbsolute(rect);
			if (getWrapLabel().isTextWrapOn()
					&& getWrapLabel().getText().length() > 0) {
				rect.setSize(new Dimension(text.computeSize(rect.width,
						SWT.DEFAULT)));
			} else {
				int avr = FigureUtilities.getFontMetrics(text.getFont())
						.getAverageCharWidth();
				rect.setSize(new Dimension(text.computeSize(SWT.DEFAULT,
						SWT.DEFAULT)).expand(avr * 2, 0));
			}
			if (!rect.equals(new Rectangle(text.getBounds()))) {
				text.setBounds(rect.x, rect.y, rect.width, rect.height);
			}
		}
	}

	/**
	 * @generated
	 */
	private static class LabelCellEditorLocator implements CellEditorLocator {

		/**
		 * @generated
		 */
		private Label label;

		/**
		 * @generated
		 */
		public LabelCellEditorLocator(Label label) {
			this.label = label;
		}

		/**
		 * @generated
		 */
		public Label getLabel() {
			return label;
		}

		/**
		 * @generated
		 */
		public void relocate(CellEditor celleditor) {
			Text text = (Text) celleditor.getControl();
			Rectangle rect = getLabel().getTextBounds().getCopy();
			getLabel().translateToAbsolute(rect);
			int avr = FigureUtilities.getFontMetrics(text.getFont())
					.getAverageCharWidth();
			rect.setSize(new Dimension(text.computeSize(SWT.DEFAULT,
					SWT.DEFAULT)).expand(avr * 2, 0));
			if (!rect.equals(new Rectangle(text.getBounds()))) {
				text.setBounds(rect.x, rect.y, rect.width, rect.height);
			}
		}
	}
}
